﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Linq;
using System.ComponentModel;
namespace GenIX.StyleSpace
{

    //<STYLET>Отвечает за Безошибочное преобразование типов</STYLET>
    class StyleT
    {
        public StyleT() : this("Microsoft Sans Serif", "Regular", "Black", "8", "WhiteSmoke") { }
        public StyleT(string fontFamalyS, string fontStyleS, string foreColorS, string fontSizeS, string colorS)
        {
            this.foreColorS = ConvertForeColor(foreColorS);
            this.colorS = ConvertColor(colorS);
            this.fontSizeS = ConvertFontSize(fontSizeS);
            this.fontFamalyS = ConvertFont(fontFamalyS);
            this.fontStyleS = ConvertFontStyle(fontStyleS);

            this.FontS = new Font(this.fontFamalyS, this.fontSizeS, this.fontStyleS);
            this.Err = "";

        }
        //<TYPE> ТИП ПО УМОЛЧАНИЮ</TYPE>
        private string foreColorT = "Black";
        private string colorT = "WhiteSmoke";
        private string fontSizeT = "8";
        private string fontFamalyT = "Microsoft Sans Serif";
        private string fontStyleT = "Regular";

        public string Err;
        public Color foreColorS;
        public Color colorS;
        public int fontSizeS;
        public string fontFamalyS;
        public FontStyle fontStyleS;
        public Font FontS;
        private string ConvertFont(string fontFamalyT)
        {
            string temp = "";
            var query = from table in FontFamily.Families.Where(k => k.Name == fontFamalyT) select new { Name = table.Name };
            foreach (var item in query) temp = item.Name;
            if (temp != "")
            {
                this.fontFamalyT = temp;
                return this.fontFamalyT;
            }
            else
            {
                this.Err += "   -ошибка преобразования текста в тип шрифт\r\n";

            }
            return this.fontFamalyT;
        }
        private FontStyle ConvertFontStyle(string fontStyleT)
        {
            try
            {
                FontStyle temp = (FontStyle)TypeDescriptor.GetConverter(fontStyleS).ConvertFrom(fontStyleT);
                this.fontStyleT = fontStyleT;
                return temp;
            }
            catch
            {
                this.Err += "   -ошибка преобразования текста в тип стиль шрифта\r\n";

            }
            return ConvertFontStyle(this.fontStyleT);
        }
        private int ConvertFontSize(string fontSizeT)
        {
            try
            {
                int temp = Int32.Parse(fontSizeT);
                this.fontSizeT = fontSizeT;
                return temp;
            }
            catch
            {
                this.Err += "   -ошибка преобразования текста в тип число\r\n";
                MessageBox.Show("err");
            }
            return ConvertFontSize(this.fontSizeT);
        }
        private Color ConvertColor(string colorT)
        {
            ColorConverter convert = new ColorConverter();
            try
            {
                Color temp = ((Color)convert.ConvertFrom(colorT));
                this.colorT = colorT;
                return temp;
            }
            catch
            {
                this.Err += "   -ошибка преобразования текста в тип цвет фона\r\n";
                MessageBox.Show("err");
            }
            return ConvertColor(this.colorT);
        }
        private Color ConvertForeColor(string foreColorT)
        {
            ColorConverter convert = new ColorConverter();
            try
            {
                Color temp = ((Color)convert.ConvertFrom(foreColorT));
                this.foreColorT = foreColorT;
                return temp;
            }
            catch
            {
                this.Err += "   -ошибка преобразования текста в тип цвет шрифта\r\n";
                MessageBox.Show("err");
            }
            return ConvertColor(this.foreColorT);
        }

        public string FontFamalyT
        {
            get { return fontFamalyT; }
            set
            {
                this.fontFamalyS = ConvertFont(value);
                this.FontS = new Font(this.fontFamalyS, this.fontSizeS, this.fontStyleS);
            }
        }
        public string FontStyleT
        {
            get { return fontStyleT; }
            set
            {
                this.fontStyleS = ConvertFontStyle(value);
                this.FontS = new Font(this.fontFamalyS, this.fontSizeS, this.fontStyleS);
            }
        }
        public string FontSizeT
        {
            get { return fontSizeT; }
            set
            {
                this.fontSizeS = ConvertFontSize(value);
                this.FontS = new Font(this.fontFamalyS, this.fontSizeS, this.fontStyleS);
            }
        }
        public string ColorT
        {
            get { return colorT; }
            set
            {
                this.colorS = ConvertColor(value);
            }
        }
        public string ForeColorT
        {
            get { return foreColorT; }
            set
            {
                this.foreColorS = ConvertForeColor(value);
            }
        }
    }

    class SButton : StyleT
    {
        public SButton() : base() { }
        public SButton(string fontFamalyS, string fontStyleS, string foreColorS, string fontSizeS, string colorS)
            : base("Microsoft Sans Serif", "Regular", "Black", "8", "WhiteSmoke")
        {
        }
        public bool check = false;
        public static SButton ReadElement(IEnumerable<XElement> queryStyle, string nameStyle)
        {
            var queryElement = from table in
                                   queryStyle.Elements("Sbutton")
                               // <GET_STRUCT>получаем структуру элемента</GET_STRUCT>
                               select table;
            SButton tempOBJ = new SButton();
            tempOBJ.Err += "-элемент стиля:Sbutton\n\r";
            string str = "";
            //  <CYCLE>так как элементов  типа Sbutton может быть более чем 1 проходим циклам</CYCLE>
            foreach (var elements in queryElement)
            {
                tempOBJ.check = true;
                XElement element = (XElement)elements;
                //<FIRST_NODE> получаем первый узел  конкретной структуры </FIRST_NODE>
                XNode node = element.FirstNode;
                while (node != null)
                {
                    //<IS_NODE> если узел есть элемент то бегаем по свойствам </IS_NODE>
                    if (node.NodeType == XmlNodeType.Element)
                    {

                        XElement property = (XElement)node;
                        str = property.Name.ToString();
                        switch (str)
                        {
                            case "color":
                                tempOBJ.ColorT = property.Value;
                                break;
                            case "font":
                                tempOBJ.FontFamalyT = property.Value;
                                break;
                            case "fontSize":
                                tempOBJ.FontSizeT = property.Value;
                                break;
                            case "foreColor":
                                tempOBJ.ForeColorT = property.Value;
                                break;
                            case "fontStyle":
                                tempOBJ.FontStyleT = property.Value;
                                break;
                            default:
                                break;
                        }
                    }
                    node = node.NextNode;
                }
            }
            return tempOBJ;
        }
        public Button ConvertToButton(Button bt)
        {
            bool currE = bt.Enabled;
            bt.Enabled = true;
            bt.BackColor = this.colorS;
            bt.Font = this.FontS;
            bt.ForeColor = this.foreColorS;
            bt.Enabled = currE;
            return bt;
        }
    }

    class SForm : StyleT
    {
        public SForm() : base() { }
        public SForm(string fontFamalyS, string fontStyleS, string foreColorS, string fontSizeS, string colorS)
            : base("Microsoft Sans Serif", "Regular", "Black", "8", "WhiteSmoke")
        {

        }
        public bool check = false;
        public static SForm ReadElement(IEnumerable<XElement> queryStyle, string nameStyle)
        {
            //<GET_QUERY> получаем конкретный стиль</GET_QUERY>
            var queryElement = from table in
                                   queryStyle.Elements("Sform")
                               //<GET_EL_STRUCT> получаем структуру элемента</GET_EL_STRUCT>
                               select table;
            SForm tempOBJ = new SForm();
            tempOBJ.Err += "-элемент стиля:Sform\n\r";
            string str = "";
            //<MORE_SB>  так как элемента типа Sbutton могут быть более чем 1 проходим циклам</MORE_SB>
            foreach (var elements in queryElement)
            {
                tempOBJ.check = true;
                XElement element = (XElement)elements;
                //<FIRST_NODE> получаем первый узел  конкретной структуры </FIRST_NODE>
                XNode node = element.FirstNode;
                while (node != null)
                {

                    if (node.NodeType == XmlNodeType.Element)
                    {
                        XElement property = (XElement)node;
                        str = property.Name.ToString();
                        switch (str)
                        {
                            case "color":
                                tempOBJ.ColorT = property.Value;
                                break;
                            case "font":
                                tempOBJ.FontFamalyT = property.Value;
                                break;
                            case "fontSize":
                                tempOBJ.FontSizeT = property.Value;
                                break;
                            case "foreColor":
                                tempOBJ.ForeColorT = property.Value;
                                break;
                            case "fontStyle":
                                tempOBJ.FontStyleT = property.Value;
                                break;
                            default:
                                break;
                        }
                    }
                    node = node.NextNode;
                }
            }
            return tempOBJ;
        }
        public Form ConvertToForm(Form fr)
        {

            bool currE = fr.Enabled;
            fr.Enabled = true;
            fr.BackColor = this.colorS;
            fr.Font = this.FontS;
            fr.ForeColor = this.foreColorS;
            fr.Enabled = currE;
            return fr;
        }
    }

    class SEdit : StyleT
    {
        public SEdit() : base() { }
        public SEdit(string fontFamalyS, string fontStyleS, string foreColorS, string fontSizeS, string colorS)
            : base("Microsoft Sans Serif", "Regular", "Black", "8", "WhiteSmoke")
        {

        }
        public bool check = false;
        public static SEdit ReadElement(IEnumerable<XElement> queryStyle, string nameStyle)
        {
            var queryElement = from table in
                                   queryStyle.Elements("Sedit")
                               select table;
            SEdit tempOBJ = new SEdit();
            tempOBJ.Err += "-элемент стиля:Sedit\n\r";
            string str = "";
            foreach (var elements in queryElement)
            {
                tempOBJ.check = true;
                XElement element = (XElement)elements;
                XNode node = element.FirstNode;
                while (node != null)
                {
                    if (node.NodeType == XmlNodeType.Element)
                    {
                        XElement property = (XElement)node;
                        str = property.Name.ToString();
                        switch (str)
                        {
                            case "color":
                                tempOBJ.ColorT = property.Value;
                                break;
                            case "font":
                                tempOBJ.FontFamalyT = property.Value;
                                break;
                            case "fontSize":
                                tempOBJ.FontSizeT = property.Value;
                                break;
                            case "foreColor":
                                tempOBJ.ForeColorT = property.Value;
                                break;
                            case "fontStyle":
                                tempOBJ.FontStyleT = property.Value;
                                break;
                            default:
                                break;
                        }
                    }
                    node = node.NextNode;
                }
            }
            return tempOBJ;
        }

        public TextBox ConvertToTextBox(TextBox tb)
        {
            bool currE = tb.Enabled;
            tb.Enabled = true;
            tb.BackColor = this.colorS;
            tb.Font = this.FontS;
            tb.ForeColor = this.foreColorS;
            tb.Enabled = currE;
            return tb;
        }
        public ComboBox ConvertToTextBox(ComboBox tb)
        {

            bool currE = tb.Enabled;
            tb.Enabled = true;
            tb.BackColor = this.colorS;
            tb.Font = this.FontS;
            tb.ForeColor = this.foreColorS;
            tb.Enabled = currE;
            return tb;
        }
        public MaskedTextBox ConvertToTextBox(MaskedTextBox tb)
        {
            bool currE = tb.Enabled;
            tb.Enabled = true;
            tb.BackColor = this.colorS;
            tb.Font = this.FontS;
            tb.ForeColor = this.foreColorS;
            tb.Enabled = currE;
            return tb;
        }
        public DateTimePicker ConvertToDateTimePicker(DateTimePicker tb)
        {
            bool currE = tb.Enabled;
            tb.Enabled = true;
            tb.BackColor = this.colorS;
            tb.Font = this.FontS;
            tb.ForeColor = this.foreColorS;
            tb.Enabled = currE;
            return tb;
        }

    }

    class SMenu : StyleT
    {
        public SMenu() : base() { }
        public SMenu(string fontFamalyS, string fontStyleS, string foreColorS, string fontSizeS, string colorS)
            : base("Microsoft Sans Serif", "Regular", "Black", "8", "WhiteSmoke")
        {

        }
        public bool check = false;
        public static SMenu ReadElement(IEnumerable<XElement> queryStyle, string nameStyle)
        {
            var queryElement = from table in
                                   queryStyle.Elements("Smenu")
                               select table;
            SMenu tempOBJ = new SMenu();
            tempOBJ.Err += "-элемент стиля:Smenu\n\r";
            string str = "";
            foreach (var elements in queryElement)
            {
                tempOBJ.check = true;
                XElement element = (XElement)elements;
                XNode node = element.FirstNode;
                while (node != null)
                {
                    if (node.NodeType == XmlNodeType.Element)
                    {
                        XElement property = (XElement)node;
                        str = property.Name.ToString();
                        switch (str)
                        {
                            case "color":
                                tempOBJ.ColorT = property.Value;
                                break;
                            case "font":
                                tempOBJ.FontFamalyT = property.Value;
                                break;
                            case "fontSize":
                                tempOBJ.FontSizeT = property.Value;
                                break;
                            case "foreColor":
                                tempOBJ.ForeColorT = property.Value;
                                break;
                            case "fontStyle":
                                tempOBJ.FontStyleT = property.Value;
                                break;
                            default:
                                break;
                        }
                    }
                    node = node.NextNode;
                }
            }
            return tempOBJ;
        }
        public MenuStrip ConvertToMenu(MenuStrip mn)
        {
            bool currE = mn.Enabled;
            mn.Enabled = true;
            mn.BackColor = this.colorS;
            mn.Font = this.FontS;
            mn.ForeColor = this.foreColorS;
            mn.Enabled = currE;
            return mn;
        }
    }

    class SLabel : StyleT
    {
        public SLabel() : base() { }
        public SLabel(string fontFamalyS, string fontStyleS, string foreColorS, string fontSizeS, string colorS)
            : base("Microsoft Sans Serif", "Regular", "Black", "8", "WhiteSmoke")
        {

        }
        public bool check = false;
        public static SLabel ReadElement(IEnumerable<XElement> queryStyle, string nameStyle)
        {
            var queryElement = from table in
                                   queryStyle.Elements("Slabel")
                               select table;
            SLabel tempOBJ = new SLabel();
            tempOBJ.Err += "-элемент стиля:Slabel\n\r";
            string str = "";
            foreach (var elements in queryElement)
            {
                tempOBJ.check = true;
                XElement element = (XElement)elements;
                XNode node = element.FirstNode;
                while (node != null)
                {
                    if (node.NodeType == XmlNodeType.Element)
                    {
                        XElement property = (XElement)node;
                        str = property.Name.ToString();
                        switch (str)
                        {
                            case "color":
                                tempOBJ.ColorT = property.Value;
                                break;
                            case "font":
                                tempOBJ.FontFamalyT = property.Value;
                                break;
                            case "fontSize":
                                tempOBJ.FontSizeT = property.Value;
                                break;
                            case "foreColor":
                                tempOBJ.ForeColorT = property.Value;
                                break;
                            case "fontStyle":
                                tempOBJ.FontStyleT = property.Value;
                                break;
                            default:
                                break;
                        }
                    }
                    node = node.NextNode;
                }
            }
            return tempOBJ;
        }
        public Label ConvertToLabel(Label lb)
        {
            bool currE = lb.Enabled;
            lb.Enabled = true;
            lb.BackColor = this.colorS;
            lb.Font = this.FontS;
            lb.ForeColor = this.foreColorS;
            lb.Enabled = currE;
            return lb;
        }

    }
}
